package io.sage.modules.statistics.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.sage.common.utils.CRC16;
import io.sage.modules.sensor.entity.SysSensorConfiguration;
import io.sage.modules.sensor.service.SensorConfigurationService;
import io.sage.modules.statistics.entity.BStatisticsHumidity;
import io.sage.modules.statistics.entity.BStatisticsNoise;
import io.sage.modules.statistics.entity.BStatisticsPmTen;
import io.sage.modules.statistics.entity.BStatisticsPmTwo;
import io.sage.modules.statistics.entity.BStatisticsPressure;
import io.sage.modules.statistics.entity.BStatisticsTemperature;
import io.sage.modules.statistics.entity.BStatisticsWindDirection;
import io.sage.modules.statistics.entity.BStatisticsWindSpeed;
import io.sage.modules.statistics.service.BStatisticsHumidityService;
import io.sage.modules.statistics.service.BStatisticsNoiseService;
import io.sage.modules.statistics.service.BStatisticsPmTenService;
import io.sage.modules.statistics.service.BStatisticsPmTwoService;
import io.sage.modules.statistics.service.BStatisticsPressureService;
import io.sage.modules.statistics.service.BStatisticsTemperatureService;
import io.sage.modules.statistics.service.BStatisticsWindDirectionService;
import io.sage.modules.statistics.service.BStatisticsWindSpeedService;
import io.sage.modules.statistics.service.WeatherService;

/**
 * 庭院的实时天气service
 * @author xjh
 *
 */
@Service("weatherService")
public class WeatherServiceImpl implements WeatherService {
	
	@Autowired
	private BStatisticsNoiseService statisticsNoiseService;
	
	@Autowired
	private SensorConfigurationService sensorConfigurationService;
	
	@Autowired
	private BStatisticsHumidityService statisticsHumidityService;
	
	@Autowired
	private BStatisticsPmTenService statisticsPmTenService;
	
	@Autowired
	private BStatisticsPmTwoService statisticsPmTwoService;
	
	@Autowired
	private BStatisticsPressureService statisticsPressureService;
	
	@Autowired
	private BStatisticsTemperatureService statisticsTemperatureService;
	
	@Autowired
	private BStatisticsWindDirectionService statisticsWindDirectionService;
	
	@Autowired
	private BStatisticsWindSpeedService statisticsWindSpeedService;

	@Override
	public int insert(String weatherData) {
		if("00000000000000000000".equals(weatherData.substring(10, 30))){
		//获取CRC16校验值.
		String crc16 = CRC16.CRC16(CRC16.hex_to_ten(weatherData.substring(0, 58)));
		
		if(crc16.compareToIgnoreCase(weatherData.substring(58, 62))==0){
			
			String querySensor="FF030001000DC011";
			SysSensorConfiguration sensorConfiguration = sensorConfigurationService.selectByquerySensor(querySensor);
			/*** 噪音的数据添加开始*/
			BStatisticsNoise statisticsNoise = new BStatisticsNoise();
			float parseLong = (float)Long.parseLong(weatherData.substring(6, 10), 16)/10;
			statisticsNoise.setValue((double)parseLong);
			statisticsNoise.setUnit("dB");
			statisticsNoise.setMachineId(sensorConfiguration.getId());
			statisticsNoiseService.insert(statisticsNoise);
			/*** 噪音的数据添加结束*/
			
			/**湿度添加数据开始*/
			BStatisticsHumidity statisticsHumidity=new BStatisticsHumidity();
			float humidity = (float)Long.parseLong(weatherData.substring(42, 46), 16)/100;
			statisticsHumidity.setValue((double)humidity);
			statisticsHumidity.setUnit("%");
			statisticsHumidity.setMachineId(sensorConfiguration.getId());
			statisticsHumidityService.insert(statisticsHumidity);
			/**湿度添加数据结束*/
			
			/**PM10数据添加开始*/
			BStatisticsPmTen statisticsPmTen=new BStatisticsPmTen();
			float pmTen = (float)Long.parseLong(weatherData.substring(34, 38), 16);
			statisticsPmTen.setValue((double)pmTen);
			statisticsPmTen.setUnit("ppm");
			statisticsPmTen.setMachineId(sensorConfiguration.getId());
			statisticsPmTenService.insert(statisticsPmTen);
			/**PM10数据添加结束*/
			
			/**PM2.5数据添加开始*/
			BStatisticsPmTwo statisticsPmTwo=new BStatisticsPmTwo();
			float pmTwo = (float)Long.parseLong(weatherData.substring(30, 34), 16);
			statisticsPmTwo.setValue((double)pmTwo);
			statisticsPmTwo.setUnit("ppm");
			statisticsPmTwo.setMachineId(sensorConfiguration.getId());
			statisticsPmTwoService.insert(statisticsPmTwo);
			/**PM2.5数据添加结束*/
			
			/**大气压强数据添加开始*/
			BStatisticsPressure statisticsPressure=new BStatisticsPressure();
			float pressure = (float)Long.parseLong(weatherData.substring(46, 50), 16)/10;
			statisticsPressure.setValue((double)pressure);
			statisticsPressure.setUnit("hPa");
			statisticsPressure.setMachineId(sensorConfiguration.getId());
			statisticsPressureService.insert(statisticsPressure);
			/**大气压强数据添结束*/
			
			/**大气气温数据添加开始*/
			BStatisticsTemperature statisticsTemperature=new BStatisticsTemperature();
			float temperature = (float)Long.parseLong(weatherData.substring(38, 42), 16)/100-40;
			statisticsTemperature.setValue((double)temperature);
			statisticsTemperature.setUnit("℃");
			statisticsTemperature.setMachineId(sensorConfiguration.getId());
			statisticsTemperatureService.insert(statisticsTemperature);
			/**大气气温数据添结束*/
			
			/**风向数据添加开始*/
			BStatisticsWindDirection statisticsWindDirection=new BStatisticsWindDirection();
			float windDirection = (float)Long.parseLong(weatherData.substring(54, 58), 16)/10;
			statisticsWindDirection.setValue((double)windDirection);
			statisticsWindDirection.setUnit("°");
			statisticsWindDirection.setMachineId(sensorConfiguration.getId());
			statisticsWindDirectionService.insert(statisticsWindDirection);
			/**风向数据添结束*/
			
			/**风速数据添加开始*/
			BStatisticsWindSpeed statisticsWindSpeed=new BStatisticsWindSpeed();
			float windSpeed = (float)Long.parseLong(weatherData.substring(50, 54), 16)/100;
			statisticsWindSpeed.setValue((double)windSpeed);
			statisticsWindSpeed.setUnit("m/s");
			statisticsWindSpeed.setMachineId(sensorConfiguration.getId());
			statisticsWindSpeedService.insert(statisticsWindSpeed);
			/**风速数据添加结束*/
			}
		
		}
		return 0;
	}

}
